package  com.syqc.sensor.recent

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.OilDay
import com.syqc.monitor.R
import com.syqc.utils.Units

class RecentOilInfoAdapter(private val teamName: String) :
    BaseQuickAdapter<OilDay, BaseViewHolder>(R.layout.item_sensor_day_oil) {
    var precision: String = "2"
    var oilUnit: String = ""
    private val mileUnit: String by lazy { context.getString(R.string.unit_mile) }
    private val riceUnit: String by lazy { context.getString(R.string.unit_rice) }
    private val unitTime by lazy { context.getString(R.string.unit_num) }

    init {
        addChildClickViewIds(R.id.ivDayInfo, R.id.lrvOilAdd, R.id.lrvOilLeak)
    }

    override fun convert(holder: BaseViewHolder, item: OilDay) {
        val position = holder.layoutPosition + 1
        val num = if (position < 10) "0$position" else "$position"
        holder.setText(R.id.tvItemNum, num)
        holder.run {
            item.run {
                val addTotal = getView<LeftRightTextView>(R.id.lrvOilAdd)
                addTotal.setRightText("$upTimes$unitTime  $upTotal$unit")
                addTotal.nextVisible(upTimes > 0)
                addTotal.isEnabled = upTimes > 0

                val leak = getView<LeftRightTextView>(R.id.lrvOilLeak)
                leak.setRightText("$leakTimes$unitTime  $leakTotal$unit")
                leak.nextVisible(leakTimes > 0)
                leak.isEnabled = leakTimes > 0

                val mileStr = if (mile > 1000) {
                    "$mileKm $mileUnit"
                    "$mileKm ${Units.km}"
                } else {
                    "$mile $riceUnit"
                    "$mile ${Units.m}"
                }

                getView<LeftRightTextView>(R.id.lrvOilTeam).setRightText(teamName)
                getView<LeftRightTextView>(R.id.lrvOilRun).setRightText("$runTotal$unit")
                getView<LeftRightTextView>(R.id.lrvOilDate).setRightText(day)
                getView<LeftRightTextView>(R.id.lrvOilDown).setRightText("$downTotal $oilUnit")
                getView<LeftRightTextView>(R.id.lrvOilSpeed).setRightText("$idlingTotal $oilUnit")
                getView<LeftRightTextView>(R.id.lrvOilMile).setRightText(mileStr)
                getView<LeftRightTextView>(R.id.lrvOilTotal100).setRightText("$totalBy100km$unit")
                getView<LeftRightTextView>(R.id.lrvOilRun100).setRightText("$runBy100km $oilUnit")
            }
        }
    }
}
